package top.wineye.spring.ioc.container;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import top.wineye.model.User;

public class BeanFactoryAsContainer {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory =
                new DefaultListableBeanFactory();

        /*
            XmlBeanDefinitionReader 是一个 BeanDefinitionReader 的实现
            它需要持有一个 beanDefinitionRegistry， 也就是 read BeanDefinition 到这个 registry 中
            BeanDefinition 就是字面意思 bean 的定义，相当于 bean 的元信息
            同时 DefaultListableBeanFactory 也是一个 BeanDefinitionRegistry
        */
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        String location = "classpath:META-INF/dependency-lookup-context.xml";
        int loadedBeanDefinitionCount = reader.loadBeanDefinitions(location);
        System.out.println("load bean definition count: " + loadedBeanDefinitionCount);

        // get bean
        User user = beanFactory.getBean(User.class);
        System.out.println("get bean by type User.class :" + user);
    }
}
